import { useEffect, useState } from 'react'
import { useRecoilValue } from 'recoil'
import { message } from 'antd'
import { platformConfigType, publicNoticeConfigType, instructionsConfigType, tableData } from '../interface/interface'
import { getPlatformSetList, addPlatformSetList, deletePlatformSetList } from '../service/service'
import { platformDetailsInfoSelect } from '@/store/platformDetails'
import { isNullData } from '@/utils'

export default () => {
  const platformDetailsInfoValue = useRecoilValue(platformDetailsInfoSelect)

  // 存所有表格数据，动态获取
  const [tableData, setTableData] = useState<tableData[]>([])
  // tabs 受控
  const [tabsActiveKey, setTabsActiveKey] = useState('1')

  // 获取表格数据
  const getTableData = async () => {
    const params = {
      searchCount: true,
      search: {
        platformId: platformDetailsInfoValue.platformId,
        type: tabsActiveKey,
      },
      pageSize: 3960624,
      pageIndex: -45053152,
    }
    const res = await getPlatformSetList(params)
    if (isNullData(res)) {
      setTableData(res.data.items)
    }
  }

  // 切换 tabs 事件
  const changeTabs = async (value: string) => {
    setTabsActiveKey(value)
  }

  // 平台概况
  // #region
  const [platformOverview, setPlatformOverview] = useState<string>('')
  // 平台 - 中心任务文字 和 领导团队文字
  const [platformModalFont, setPlatformModalFont] = useState({
    platFormCenter: '',
    platFormBossTeam: '',
  })
  // 拿到平台概况富文本输入内容
  const getPlatformTextFont = (value: string) => {
    setPlatformOverview(value)
  }
  // 平台概况弹框
  const [platformOpen, setPlatformlOpen] = useState(false)
  // 打开弹框
  const showPlatFormOpen = () => {
    setPlatformlOpen(true)
  }
  // 关闭弹框
  const clearPlatFormOpen = () => {
    setPlatformlOpen(false)
  }
  // 点击查看打开弹框
  const platformExamineShowOpen = (record: any) => {
    console.log(record)
    setPlatformlOpen(true)
    setPlatformModalFont({
      ...platformModalFont,
      platFormCenter: record.content,
    })
  }
  // 平台概况保存
  const platformClickSave = async () => {
    if (platformOverview === '') {
      message.error('请输入内容之后保存')
      return
    }
    const params = {
      type: 1,
      content: platformOverview,
    }
    const res = await addPlatformSetList(params)
    if (isNullData(res)) {
      message.success('新增成功')
      getTableData()
      setPlatformOverview('')
    }
    console.log('🦢🦢', res)
  }

  // 平台概况表格删除
  const platformDeleteData = async (record: any) => {
    const res = await deletePlatformSetList({ id: record.id })
    if (isNullData(res)) {
      message.success('删除成功')
      getTableData()
    }
  }
  // #endregion

  // 操作指南
  // #region
  const [instructionsOverview, setinstructionsOverview] = useState<string>('')
  // 平台 - 中心任务文字 和 领导团队文字
  const [instructionsModalFont, setInstructionsModalFont] = useState({
    instructionsCenter: '',
    instructionsBossTeam: '',
  })
  // 拿到操作指南富文本输入内容
  const getInstructionsTextFont = (value: string) => {
    setinstructionsOverview(value)
  }
  // 操作指南弹框
  const [instructionsOpen, setInstructionsOpen] = useState(false)
  // 操作指南打开弹框
  const showInstructionsOpen = () => {
    setInstructionsOpen(true)
  }
  // 操作指南关闭弹框
  const clearInstructionsOpen = () => {
    setInstructionsOpen(false)
  }
  // 点击操作指南底部表格查看打开弹框
  const instructionsExamineShowOpen = (record: any) => {
    console.log(record)
    setInstructionsOpen(true)
    setInstructionsModalFont({
      ...instructionsModalFont,
      instructionsCenter: record.content,
    })
  }
  // 操作指南保存
  const instructionsClickSave = async () => {
    if (instructionsOverview === '') {
      message.error('请输入内容之后保存')
      return
    }
    const params = {
      type: 2,
      content: instructionsOverview,
    }
    const res = await addPlatformSetList(params)
    if (isNullData(res)) {
      message.success('新增成功')
      getTableData()
      setinstructionsOverview('')
    }
  }
  // 操作指南删除
  const deleteInstructions = async (record: any) => {
    const res = await deletePlatformSetList({ id: record.id })
    if (isNullData(res)) {
      message.success('删除成功')
      getTableData()
    }
  }
  // #endregion

  // 公告通知
  // #region
  const [publicNoticeOverview, setPublicNoticeOverview] = useState<string>('')
  // 公告通知 - 中心任务文字 和 领导团队文字
  const [publicNoticeModalFont, setPublicNoticeModalFont] = useState({
    publicNoticeCenter: '',
    publicNoticeBossTeam: '',
  })
  // 拿到公告通知富文本输入内容
  const getPublicNoticeTextFont = (value: string) => {
    setPublicNoticeOverview(value)
  }
  // 公告通知弹框
  const [publicNoticeOpen, setPublicNoticeOpen] = useState(false)
  // 公告通知打开弹框
  const showPublicNoticeOpen = () => {
    setPublicNoticeOpen(true)
  }
  // 公告通知关闭弹框
  const clearPublicNoticeOpen = () => {
    setPublicNoticeOpen(false)
  }
  // 点击公告通知底部表格查看打开弹框
  const publicNoticeExamineShowOpen = (record: any) => {
    console.log(record)
    setPublicNoticeOpen(true)
    setPublicNoticeModalFont({
      ...publicNoticeModalFont,
      publicNoticeCenter: record.content,
    })
  }
  // 公告通知保存
  const publicNoticeClickSave = async () => {
    if (publicNoticeOverview === '') {
      message.error('请输入内容之后保存')
      return
    }
    const params = {
      type: 3,
      content: publicNoticeOverview,
    }
    const res = await addPlatformSetList(params)
    if (isNullData(res)) {
      message.success('新增成功')
      getTableData()
      setPublicNoticeOverview('')
    }
  }

  // 公告通知删除
  const publicNoticeDeleteData = async (record: any) => {
    const res = await deletePlatformSetList({ id: record.id })
    if (isNullData(res)) {
      message.success('删除成功')
      getTableData()
    }
  }
  // #endregion

  useEffect(() => {
    getTableData()
  }, [tabsActiveKey])

  // 平台概况配置
  const platformConfig: platformConfigType = {
    platformOverview,
    platformOpen,
    platformModalFont,
    showPlatFormOpen,
    clearPlatFormOpen,
    getPlatformTextFont,
    platformExamineShowOpen,
    changeTabs,
    platformClickSave,
    platformDeleteData,
    tabsActiveKey,
  }

  // 操作指南
  const instructionsConfig: instructionsConfigType = {
    instructionsOverview,
    instructionsModalFont,
    instructionsOpen,
    getInstructionsTextFont,
    showInstructionsOpen,
    clearInstructionsOpen,
    instructionsExamineShowOpen,
    instructionsClickSave,
    deleteInstructions,
  }

  // 公告通知
  const publicNoticeConfig: publicNoticeConfigType = {
    publicNoticeOverview,
    publicNoticeModalFont,
    publicNoticeOpen,
    getPublicNoticeTextFont,
    showPublicNoticeOpen,
    clearPublicNoticeOpen,
    publicNoticeExamineShowOpen,
    publicNoticeClickSave,
    publicNoticeDeleteData,
  }

  // 表格数据
  const tableDataType = {
    tableData,
  }

  return {
    platformConfig,
    instructionsConfig,
    publicNoticeConfig,
    tableDataType,
  }
}
